package com.example.teacher.controller;


import com.example.commonutils.ResultData;
import com.example.teacher.entity.EduChapter;
import com.example.teacher.entity.chapter.ChapterVo;
import com.example.teacher.service.EduChapterService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-05-25
 */
@RestController
@RequestMapping("/backstage/chapter")
public class EduChapterController {
    @Autowired
    private EduChapterService chapterService;

    @GetMapping(value = "getChapter/{id}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultData getChapter(@PathVariable String id) {
        List<ChapterVo> list = chapterService.getChapterVideo(id);
        return ResultData.ok().data("allChapterVideo",list);
    }
    //添加章节信息
    @PostMapping(value = "addChapterInfo",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultData addChapterInfo(@RequestBody EduChapter eduChapter){
        chapterService.save(eduChapter);
        return ResultData.ok();
    }
    //根据id查询章节信息
    @GetMapping(value = "getChapterById/{id}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultData getChapterById(@PathVariable String id){
        EduChapter byId = chapterService.getById(id);
        return ResultData.ok().data("chapter",byId);
    }
    //修改章节信息
    @PostMapping(value = "updateChapterInfo",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultData updateChapterInfo(@RequestBody EduChapter eduChapter){
        chapterService.updateById(eduChapter);
        return ResultData.ok();
    }

    //删除章节信息
    @DeleteMapping(value = "deleteChapter/{id}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultData deleteChapter(@PathVariable String id){
        boolean bool=chapterService.deleteChapter(id);
        if (bool){
            return ResultData.ok();
        }
        return ResultData.error();
    }
}

